{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Smallest Range I"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #数学"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: smallestRangeI"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #最小差值 I"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数数组 <code>nums</code>，和一个整数 <code>k</code> 。</p>\n",
    "\n",
    "<p>在一个操作中，您可以选择 <code>0 &lt;= i &lt; nums.length</code> 的任何索引 <code>i</code> 。将 <code>nums[i]</code> 改为 <code>nums[i] + x</code> ，其中 <code>x</code> 是一个范围为 <code>[-k, k]</code> 的整数。对于每个索引 <code>i</code> ，最多 <strong>只能 </strong>应用 <strong>一次</strong> 此操作。</p>\n",
    "\n",
    "<p><code>nums</code>&nbsp;的&nbsp;<strong>分数&nbsp;</strong>是&nbsp;<code>nums</code>&nbsp;中最大和最小元素的差值。&nbsp;</p>\n",
    "\n",
    "<p><em>在对&nbsp; <code>nums</code> 中的每个索引最多应用一次上述操作后，返回&nbsp;<code>nums</code> 的最低 <strong>分数</strong></em> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [1], k = 0\n",
    "<strong>输出：</strong>0\n",
    "<strong>解释：</strong>分数是 max(nums) - min(nums) = 1 - 1 = 0。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [0,10], k = 2\n",
    "<strong>输出：</strong>6\n",
    "<strong>解释：</strong>将 nums 改为 [2,8]。分数是 max(nums) - min(nums) = 8 - 2 = 6。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [1,3,6], k = 3\n",
    "<strong>输出：</strong>0\n",
    "<strong>解释：</strong>将 nums 改为 [4,4,4]。分数是 max(nums) - min(nums) = 4 - 4 = 0。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>\n",
    "\t<li><code>0 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>\n",
    "\t<li><code>0 &lt;= k &lt;= 10<sup>4</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [smallest-range-i](https://leetcode.cn/problems/smallest-range-i/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [smallest-range-i](https://leetcode.cn/problems/smallest-range-i/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1]\\n0', '[0,10]\\n2', '[1,3,6]\\n3']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        return max(nums) - min(nums) - 2 * k if (max(nums) - min(nums) - 2 * k >= 0) else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        nums.sort()\n",
    "        left = nums[0]\n",
    "        right = max(nums)\n",
    "        mid = (left + right) // 2\n",
    "        n = len(nums)\n",
    "        ma = 0\n",
    "        mi = inf\n",
    "        for i in range(n):\n",
    "            num = nums[i]\n",
    "            if abs(mid - num) > k:\n",
    "                if mid - num < 0:\n",
    "                    num -= k\n",
    "                else:\n",
    "                    num += k\n",
    "            else:\n",
    "                num = mid\n",
    "\n",
    "            ma = max(ma, num)\n",
    "            mi = min(mi, num)\n",
    "        return ma - mi\n",
    "                    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        return max(nums) - min(nums) - 2 * k if (max(nums) - min(nums) - 2 * k >= 0) else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        return max(0, max(nums) - min(nums) - 2 * k)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        if max(nums)-min(nums)-2*k>0:\n",
    "            return max(nums)-min(nums)-2*k\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        return max(0, max(nums) - min(nums) - 2 * k)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        max, min = Solution.findMaxAndMin(nums)\n",
    "        if(max - min > 2 * k):\n",
    "            return max - min - 2*k\n",
    "        else:\n",
    "            return 0\n",
    "\n",
    "\n",
    "\n",
    "    def findMaxAndMin(nums:List[int]) -> (int,int):\n",
    "        max,min = nums[0],nums[0]\n",
    "        for num in nums:\n",
    "            if(max < num):\n",
    "                max = num\n",
    "            if(min > num):\n",
    "                min = num\n",
    "        return max,min\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        n = len(nums)\n",
    "        if n == 1: return 0\n",
    "        mx, mi = max(nums), min(nums)\n",
    "        if mx - mi <= 2 * k: return 0\n",
    "        return mx - mi - 2 * k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        nums.sort()\n",
    "        ans = nums[-1] - nums[0] - k * 2\n",
    "        return 0 if ans < 0 else ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        if max(nums)-min(nums) >= 2*k :\n",
    "            return max(nums)-min(nums)-2*k\n",
    "        else:\n",
    "            return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        if max(nums)-min(nums)<=2*k:\n",
    "            return  0\n",
    "        else:\n",
    "            return max(nums)-min(nums)-2*k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        return max(0,max(nums) - min(nums) -2*k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        a = max(nums)\n",
    "        b = min(nums)\n",
    "        ans = a - b - k * 2\n",
    "        return 0 if ans < 0 else ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        if len(nums) == 1:\n",
    "            return 0\n",
    "        nums.sort()\n",
    "        return 0 if (nums[0] + k) > (nums[-1] - k) else (nums[-1] - k) - (nums[0] + k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        return max(0, max(nums) - min(nums) - 2 * k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        a = max(nums)\n",
    "        b = min(nums)\n",
    "        if a - b <= 2*k:\n",
    "            return 0\n",
    "        else:\n",
    "            return a - b - 2*k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        return max(0, max(nums) - min(nums) - 2 * k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        a=max(nums)-min(nums)-2*k\n",
    "        if a>0:\n",
    "            return a\n",
    "        else:\n",
    "            return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        mx, mn = nums[0], nums[0]\n",
    "        for num in nums[1:]:\n",
    "            mx = max(mx, num)\n",
    "            mn = min(mn, num)\n",
    "        #\n",
    "        # if mx - mn >= 2 * k:\n",
    "        return max(mx - mn - 2 * k, 0)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        nums = sorted(nums)\n",
    "        return max(nums[-1] - k - (nums[0]+k),0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        #每个点为中心，k为半径形成一个区间；\n",
    "        #求最右侧区间的下限到最左侧区间的上限的距离，若为负则返回0；\n",
    "        #res = max[nums] - k - (min(nums) + k), 化简得max(nums) - min(nums) - 2*k\n",
    "        res = max(nums) - min(nums) - 2*k\n",
    "        return max(res, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        return max(0, max(nums) - min(nums) - 2 * k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        return max(0, max(nums) - min(nums) - k - k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        MIN = min(nums) + k\n",
    "        MAX = max(nums) - k\n",
    "        if MIN >= MAX:\n",
    "            return 0\n",
    "        else:\n",
    "            return MAX - MIN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        nums.sort()\n",
    "        return max(0, nums[len(nums)-1] - nums[0] - k * 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums, k) -> int:\n",
    "        nums.sort\n",
    "        a=min(nums)\n",
    "        b=max(nums)\n",
    "        # print(a,b)\n",
    "        if b-a>2*k:\n",
    "            return b-a-2*k\n",
    "        else:\n",
    "            return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        sub = max(nums) - min(nums)\n",
    "        return sub - 2 * k if sub > 2 * k else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        return max(0, max(nums) - min(nums) - 2*k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        mx=max(nums)\n",
    "        mi=min(nums)\n",
    "        if mx-mi<=k*2:\n",
    "            return 0\n",
    "        else:\n",
    "            return mx-mi-k*2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        nums.sort()\n",
    "        if nums[-1] - nums[0] > 2*k:\n",
    "            return nums[-1] - nums[0] - 2*k\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        return max(0,max(nums)-min(nums)-2*k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        return max(max(nums)-k*2-min(nums),0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        minValue, maxValue = min(nums), max(nums)\n",
    "        dif = maxValue - minValue\n",
    "        return 0 if 2*k > dif else dif-2*k\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "\n",
    "        d = max(nums) - min(nums)\n",
    "        if 2 * k >= d:\n",
    "            return 0\n",
    "        else:\n",
    "            return d - 2 * k "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        mx, mn = nums[0], nums[0]\n",
    "        for num in nums[1:]:\n",
    "            mx = max(mx, num)\n",
    "            mn = min(mn, num)\n",
    "        return max(mx - mn - 2 * k, 0)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        d = max(nums)-min(nums)\n",
    "        if d<=2*k:\n",
    "            return 0\n",
    "        else:\n",
    "            return d-2*k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        return max(nums) - min(nums) - k * 2 if max(nums) - min(nums) - k * 2 > 0 else 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        return max(0, max(nums) - min(nums) - 2 * k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        return max(0,max(nums) - min(nums) - 2 * k)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        if len(nums) < 2 :return 0\n",
    "        temp = sorted(nums)\n",
    "        l = temp[0]+k\n",
    "        h = temp[-1]-k\n",
    "        return h-l if h>=l else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        return max(0, max(nums) - min(nums) - 2 * k)\n",
    "                    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        nums=sorted(nums)\n",
    "        res=nums[-1]-nums[0]-2*k\n",
    "        return res if res>=0 else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        min_num = min(nums)\n",
    "        max_num = max(nums)\n",
    "        # for num in nums:\n",
    "        #     min_num = min(min_num,num)\n",
    "        #     max_num = max(max_num,num)\n",
    "        #print(max_num)\n",
    "        #print(min_num)\n",
    "        if max_num - min_num <= 2*k:\n",
    "            return 0\n",
    "        return max_num - min_num - 2*k\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        m = max(nums)\n",
    "        n = min(nums)\n",
    "        if m-n>2*k:\n",
    "            return m-k-n-k\n",
    "        return 0 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        ans = max(nums) - min(nums) - k * 2\n",
    "        return 0 if ans < 0 else ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        return max(0, max(nums) - min(nums) - 2 * k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        # 只需要考虑最大和最小就行了\n",
    "        min_x = min(nums)\n",
    "        max_x = max(nums) # 都是非负数\n",
    "\n",
    "        # 先检查差值和2k的关系\n",
    "        diff = max_x - min_x \n",
    "        if diff <= 2 * k :\n",
    "            return 0 \n",
    "        else :\n",
    "            return diff - 2 * k\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        return max(0, max(nums) - min(nums) - 2 * k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        return max(nums) - min(nums) - 2 * k if (max(nums) - min(nums) - 2 * k >= 0) else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        \"\"\"\n",
    "        1.进行排序\n",
    "        2.最小的元素加上k  最大的元素减去k\n",
    "        \"\"\"\n",
    "        nums = sorted(nums)\n",
    "        lens = len(nums)\n",
    "        if lens == 0:\n",
    "            return 0\n",
    "\n",
    "        if (nums[0] + k > nums[lens - 1] - k):\n",
    "            return 0\n",
    "        else:\n",
    "            return  nums[lens - 1] - k - nums[0] - k "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        n=len(nums)\n",
    "        if n==1:return 0\n",
    "        if k==0:return max(nums)-min(nums)\n",
    "        if max(nums)-min(nums)<=2*k:\n",
    "            return 0\n",
    "        else:\n",
    "            return max(nums)-k-(min(nums)+k)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        min_val = min(nums) + k\n",
    "        max_val = max(nums) - k\n",
    "        \n",
    "        return max(max_val - min_val, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        diff = max(nums) - min(nums)\n",
    "        if diff > 2 * k:\n",
    "            return diff - 2 * k\n",
    "        return 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        max_num, min_num = max(nums), min(nums)\n",
    "\n",
    "        return (max_num-k)-(min_num+k) if (max_num-k)-(min_num+k) > 0 else 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        return max(0, max(nums) - min(nums) - 2 * k)\n",
    "        # if diff <= 2 * k:\n",
    "        #     return 0\n",
    "        # return diff - 2 * k\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        if max(nums)-min(nums)<=2*k:\n",
    "            return 0\n",
    "        return (max(nums)-min(nums)-2*k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "      res = max(nums) - min(nums) - 2*k\n",
    "      return res if res > 0 else 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        min_num = inf\n",
    "        max_num = 0\n",
    "        for num in nums:\n",
    "            min_num = min(min_num,num)\n",
    "            max_num = max(max_num,num)\n",
    "        print(max_num)\n",
    "        print(min_num)\n",
    "        if max_num - min_num <= 2*k:\n",
    "            return 0\n",
    "        return max_num - min_num - 2*k\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        return max(max(nums)-min(nums)-2*k,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        return max(0,max(nums)-min(nums)-2*k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        mx, mn = nums[0], nums[0]\n",
    "        for num in nums[1:]:\n",
    "            if mx < num:\n",
    "                mx = num\n",
    "            if mn > num:\n",
    "                mn = num\n",
    "            # mx = max(mx, num)\n",
    "            # mn = min(mn, num)\n",
    "        return max(mx - mn - 2 * k, 0)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        nums.sort()\n",
    "        if nums[-1]-k<nums[0]+k:\n",
    "            return 0\n",
    "        else:\n",
    "            return nums[-1]-nums[0]-2*k\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\r\n",
    "        Min=min(nums)\r\n",
    "        Max=max(nums)\r\n",
    "        return max(Max-Min-2*k,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        n=len(nums)\n",
    "        if n==1:return 0\n",
    "        if k==0:return max(nums)-min(nums)\n",
    "        if max(nums)-min(nums)<=2*k:\n",
    "            return 0\n",
    "        else:\n",
    "            return max(nums)-k-(min(nums)+k)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        t = abs(max(nums) - min(nums))\n",
    "        return 0 if t < 2*k else t-2*k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        return max(0,max(nums) - min(nums) - 2*k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        a, b = max(nums), min(nums)\n",
    "        return max(0, a-b-k*2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        max=nums[0]\n",
    "        min=nums[0]\n",
    "        for i in range(len(nums)):\n",
    "            if max<nums[i]:\n",
    "                max=nums[i]\n",
    "            if min>nums[i]:\n",
    "                min=nums[i]\n",
    "        dvalue=max-min\n",
    "        if dvalue>2*k:\n",
    "            max-=k\n",
    "            min+=k\n",
    "            dvalue=max-min\n",
    "        else:\n",
    "            dvalue=0\n",
    "\n",
    "        return dvalue\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "      res = max(nums) - min(nums) - k-k\n",
    "      return res if res > 0 else 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        nums.sort()\n",
    "        if max(nums)-min(nums)<=2*k:\n",
    "            return 0\n",
    "        else:\n",
    "            return max(nums)-min(nums)-2*k\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "      res = (max(nums)-k) - (min(nums)+k)\n",
    "      if res <0:\n",
    "        return 0\n",
    "      else:\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        a,k2=max(nums)-min(nums),2*k\n",
    "        if a>k2:\n",
    "            return a-k2\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        _max,_min=max(nums),min(nums)\n",
    "        return max(_max-_min-2*k,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        a=0\n",
    "        b=10001\n",
    "        for i in nums:\n",
    "            a=max(a,i)\n",
    "            b=min(b,i)\n",
    "        c=max(a-b-2*k,0)\n",
    "        return c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        return max(0,max(nums)-min(nums)-2*k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        if len(nums) == 1:\n",
    "            return 0\n",
    "\n",
    "        if max(nums)-min(nums) <= (k*2):\n",
    "            return 0\n",
    "        else:\n",
    "            return max(nums)-min(nums)-k-k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\r\n",
    "        return max(0,max(nums)-min(nums)-2*k)\r\n",
    "        \r\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        maxNum, minNum = max(nums), min(nums)\n",
    "        if maxNum-minNum <= k*2:\n",
    "           return 0\n",
    "        else:\n",
    "            return maxNum-minNum-k*2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        min_val = min(nums)\n",
    "        max_val = max(nums)\n",
    "        if max_val - k >= min_val + k:\n",
    "            return max_val - min_val - 2*k\n",
    "        else:\n",
    "            return 0\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallestRangeI(self, nums: List[int], k: int) -> int:\n",
    "        return max(0, max(nums) - min(nums) - 2 * k)\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
